#ifndef CODEGEN_H
#define CODEGEN_H

#include "ast.h"
#include <string>
#include <unordered_map>
#include <vector>

class CodeGenerator {
private:
    std::string output;
    int labelCounter;
    std::unordered_map<std::string, int> variableOffsets;
    int stackOffset;
    
    std::string generateLabel(const std::string& prefix);
    void emit(const std::string& instruction);
    void emitLabel(const std::string& label);
    
    // Expression code generation
    void generateExpression(const Expression* expr);
    void generateBinaryExpression(const BinaryExpression* expr);
    void generateUnaryExpression(const UnaryExpression* expr);
    void generateFunctionCall(const FunctionCall* call);
    
    // Statement code generation
    void generateStatement(const Statement* stmt);
    void generateExpressionStatement(const ExpressionStatement* stmt);
    void generateReturnStatement(const ReturnStatement* stmt);
    void generateIfStatement(const IfStatement* stmt);
    void generateWhileStatement(const WhileStatement* stmt);
    void generateBlockStatement(const BlockStatement* stmt);
    
    // Declaration code generation
    void generateDeclaration(const Declaration* decl);
    void generateVariableDeclaration(const VariableDeclaration* decl);
    void generateFunctionDeclaration(const FunctionDeclaration* decl);
    
    // Utility functions
    std::string getRegisterForType(TokenType type);
    void pushToStack();
    void popFromStack();
    
public:
    CodeGenerator();
    std::string generate(const Program* program);
};

#endif
